home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000047.txt < prev    next >
Text File  |  2013-04-03  |  2KB  |  59 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Custom bindings for the tts API.
  6.  
  7. var ttsNatives = requireNative('tts');
  8. var GetNextTTSEventId = ttsNatives.GetNextTTSEventId;
  9.  
  10. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  11. var sendRequest = require('sendRequest').sendRequest;
  12. var lazyBG = requireNative('lazy_background_page');
  13.  
  14. chromeHidden.registerCustomHook('tts', function(api) {
  15.   var apiFunctions = api.apiFunctions;
  16.  
  17.   chromeHidden.tts = {
  18.     handlers: {}
  19.   };
  20.  
  21.   function ttsEventListener(event) {
  22.     var eventHandler = chromeHidden.tts.handlers[event.srcId];
  23.     if (eventHandler) {
  24.       eventHandler({
  25.                      type: event.type,
  26.                      charIndex: event.charIndex,
  27.                      errorMessage: event.errorMessage
  28.                    });
  29.       if (event.isFinalEvent) {
  30.         delete chromeHidden.tts.handlers[event.srcId];
  31.         // Balanced in 'speak' handler.
  32.         lazyBG.DecrementKeepaliveCount();
  33.       }
  34.     }
  35.   }
  36.  
  37.   // This file will get run if an extension needs the ttsEngine permission, but
  38.   // it doesn't necessarily have the tts permission. If it doesn't, trying to
  39.   // add a listener to chrome.tts.onEvent will fail.
  40.   // See http://crbug.com/122474.
  41.   try {
  42.     chrome.tts.onEvent.addListener(ttsEventListener);
  43.   } catch (e) {}
  44.  
  45.   apiFunctions.setHandleRequest('speak', function() {
  46.     var args = arguments;
  47.     if (args.length > 1 && args[1] && args[1].onEvent) {
  48.       var id = GetNextTTSEventId();
  49.       args[1].srcId = id;
  50.       chromeHidden.tts.handlers[id] = args[1].onEvent;
  51.       // Keep the page alive until the event finishes.
  52.       // Balanced in eventHandler.
  53.       lazyBG.IncrementKeepaliveCount();
  54.     }
  55.     sendRequest(this.name, args, this.definition.parameters);
  56.     return id;
  57.   });
  58. });
  59.